package com.ruoyi.system.controller;

import java.util.List;

import cn.hutool.poi.excel.ExcelReader;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FaDoorlog;
import com.ruoyi.system.service.IFaDoorlogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 考勤记录Controller
 * 
 * @author xiaohe
 * @date 2024-11-13
 */
@Controller
@RequestMapping("/system/doorlog")
public class FaDoorlogController extends BaseController
{
    private String prefix = "system/doorlog";

    @Autowired
    private IFaDoorlogService faDoorlogService;

    @RequiresPermissions("system:doorlog:view")
    @GetMapping()
    public String doorlog()
    {
        return prefix + "/doorlog";
    }

    /**
     * 查询考勤记录列表
     */
    @RequiresPermissions("system:doorlog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FaDoorlog faDoorlog)
    {
        startPage();
        List<FaDoorlog> list = faDoorlogService.selectFaDoorlogList(faDoorlog);
        return getDataTable(list);
    }

    /**
     * 导出考勤记录列表
     */
    @RequiresPermissions("system:doorlog:export")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FaDoorlog faDoorlog)
    {
        List<FaDoorlog> list = faDoorlogService.selectFaDoorlogList(faDoorlog);
        ExcelUtil<FaDoorlog> util = new ExcelUtil<FaDoorlog>(FaDoorlog.class);
        return util.exportExcel(list, "考勤记录数据");
    }

    /**
     * 新增考勤记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 导入考勤记录
     */
    @GetMapping("/import1")
    public String import1(HttpServletRequest request, ModelMap mmap)
    {
        //获取当前访问的域名和端口,给前段
        String scheme = request.getScheme(); // http or https
        String serverName = request.getServerName(); // domain name or IP address
        int serverPort = request.getServerPort(); // port number

        // 构建完整的 URL
        String baseUrl = scheme + "://" + serverName + ":" + serverPort;
        mmap.put("baseUrl", baseUrl);


        return prefix + "/import1";
    }

    /**
     * 新增保存考勤记录
     */
    @RequiresPermissions("system:doorlog:add")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FaDoorlog faDoorlog)
    {
        return toAjax(faDoorlogService.insertFaDoorlog(faDoorlog));
    }

    /**
     * 修改考勤记录
     */
    @RequiresPermissions("system:doorlog:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FaDoorlog faDoorlog = faDoorlogService.selectFaDoorlogById(id);
        mmap.put("faDoorlog", faDoorlog);
        return prefix + "/edit";
    }

    /**
     * 修改保存考勤记录
     */
    @RequiresPermissions("system:doorlog:edit")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FaDoorlog faDoorlog)
    {
        return toAjax(faDoorlogService.updateFaDoorlog(faDoorlog));
    }

    /**
     * 删除考勤记录
     */
    @RequiresPermissions("system:doorlog:remove")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(faDoorlogService.deleteFaDoorlogByIds(ids));
    }



    /**
     * 文件上传
     */
    @RequiresPermissions("system:doorlog:import1")
    @Log(title = "考勤上传", businessType = BusinessType.IMPORT)
    @PostMapping("/import1")
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file,FaDoorlog Doorlog) throws IOException {
        //打印出上传文件的路径
        System.out.println(file.getOriginalFilename());

        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        System.out.println(fileName);

        fileName = "/Users/wk/Desktop/2222.xls";

        try {
            cn.hutool.poi.excel.ExcelUtil HutuexcelUtil = new cn.hutool.poi.excel.ExcelUtil();
            ExcelReader reader = HutuexcelUtil.getReader(fileName);
            List<Map<String,Object>> readAll = reader.readAll();
            for (Map<String, Object> stringObjectMap : readAll) {
                System.out.println(stringObjectMap);
            }

//            EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
//                for (DemoData demoData : dataList) {
//                    System.out.println(demoData.getName());
//                    System.out.println(demoData.getAddr());
//                    System.out.println(demoData.getZuzhi());
//                    System.out.println(demoData.getTime());
//                }
//            })).sheet().doRead();
        }
        catch (Exception e) {
            System.out.printf(e.getMessage());
            return AjaxResult.error("读取表格失败");
        }
//        return toAjax(sysFileInfoService.insertSysFileInfo(fileInfo));

//        EasyExcel.read(file.getInputStream(), FaDoorlog.class, new UploadDataListener(faDoorlogService)).sheet().doRead();
        return AjaxResult.success("导入成功"+file.getOriginalFilename());
    }
}
